<?php
/**
 * Created by PhpStorm.
 * User: Xwen
 * Date: 2018/3/21
 * Time: 14:07
 *
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘
 *             │ ─┤ ─┤       │ ─┤ ─┤
 *             └──┴──┘       └──┴──┘
 *                 神兽保佑
 *                 代码无BUG!
 *"The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”- Seymour Cray
 */

class AuthorityController extends CommonController
{
    private $_model = NULL;
    private $_rmodel = NULL;
    private $_amodel = NULL;
    private $_urmodel = NULL;
    private $_ramodel = NULL;
    public function __auto(){
        $this->_model = K("RUser");
        $this->_rmodel = K("Role");
        $this->_amodel = K("Access");
        $this->_urmodel = K("UserRole");
        $this->_ramodel = K("RoleAccess");
    }
    public function userList(){
        $data = $this->_model->get_one_page();
        $userData = $data['returnData'];
        $this->assign('data', $data);
        $this->assign('userData', $userData);
        $this->display();
    }
    public function addUser(){
        if(IS_POST){
            $data['username'] = $_POST['username'];
            $data['password'] = md5($_POST['password']);
            $data['email'] = $_POST['email'];
            if(!$uid = $this->_model->add_data($data)){
                $this->error($this->_model->error);
            }
            if(isset($_POST['rid'])){
                foreach ($_POST['rid'] as $v){
                    $urdata['uid'] = $uid;
                    $urdata['rid'] = $v;
                    if(!$this->_urmodel->add_data($urdata)){
                        $this->error($this->_urmodel->error);
                    }
                }
            }
            $this->success('用户添加成功！',__APP__.'?c=Authority&m=userList');
        }
        $this->assign('roleData', $this->_rmodel->all());
        $this->display();
    }
    public function editUser(){
        $urData = $this->_urmodel->get_all_u();
        if (IS_POST){
            if (isset($_POST['rid'])){
                $prid = $_POST['rid'];
                $data['username'] = $_POST['username'];
                $data['email'] = $_POST['email'];
                if(!$this->_model->update_data($data)){
                    $this->error($this->_model->error);
                }
                if(!$urData){
                    foreach ($prid as $v){
                        $urdata['uid'] = $_GET['id'];
                        $urdata['rid'] = $v;
                        if(!$this->_urmodel->add_data($urdata)){
                            $this->error($this->_urmodel->error);
                        }
                    }
                }else{
                    foreach (array_column($urData, 'rid') as $v){
                        if(!in_array($v, $prid)){
                            $this->_urmodel->remove_ur($_GET['id'], $v);
                        }
                    }
                    $nurData = $this->_urmodel->get_all_u();
                    foreach ($prid as $v){
                        if(!in_array($v, array_column($nurData, 'rid'))){
                            $urdata['uid'] = $_GET['id'];
                            $urdata['rid'] = $v;
                            if(!$this->_urmodel->add_data($urdata)){
                                $this->error($this->_urmodel->error);
                            }
                        }
                    }
                }
            }else{
                foreach (array_column($urData, 'rid') as $v){
                    $this->_urmodel->remove_ur($_GET['id'], $v);
                }
            }
            $this->success('保存成功！',__APP__.'?c=Authority&m=userList');
        }
        $this->assign('rid', array_column($urData, 'rid'));
        $this->assign('roleData', $this->_rmodel->all());
        $this->assign('oldData', $this->_model->get_one());
        $this->display();
    }
    public function checkUserName(){
        if($this->_model->get_one_cn()){
            echo 0;
        }else{
            echo 1;
        }
    }
    public function checkEditUserName(){
        if($this->_model->get_one_cni()){
            echo 0;
        }else{
            echo 1;
        }
    }
    public function roleList(){
        $data = $this->_rmodel->get_one_page();
        $roleData = $data['returnData'];
        $this->assign('data', $data);
        $this->assign('roleData', $roleData);
        $this->display();
    }
    public function addRole(){
        if(IS_POST){
            if(!$this->_rmodel->add_data()){
                $this->error($this->_rmodel->error);
            }
            $this->success('角色添加成功！',__APP__.'?c=Authority&m=roleList');
        }
        $this->display();
    }
    public function editRole(){
        if (IS_POST){
            if(!$this->_rmodel->update_data()){
                $this->error($this->_rmodel->error);
            }
            $this->success('保存成功！',__APP__.'?c=Authority&m=roleList');
        }
        $this->assign('oldData', $this->_rmodel->get_one());
        $this->display();
    }
    public function setRole(){
        if(IS_POST){
            if(isset($_POST['aid'])){
                $paid = $_POST['aid'];
                if(!$this->_ramodel->get_all_r()){
                    foreach ($paid as $v){
                        $radata['rid'] = $_GET['id'];
                        $radata['aid'] = $v;
                        if(!$this->_ramodel->add_data($radata)){
                            $this->error($this->_ramodel->error);
                        }
                    }
                }else{
                    foreach (array_column($this->_ramodel->get_all_r(), 'aid') as $v){
                        if(!in_array($v, $paid)){
                            $this->_ramodel->remove_ra($_GET['id'], $v);
                        }
                    }
                    foreach ($paid as $v){
                        if(!in_array($v, array_column($this->_ramodel->get_all_r(), 'aid'))){
                            $radata['rid'] = $_GET['id'];
                            $radata['aid'] = $v;
                            if(!$this->_ramodel->add_data($radata)){
                                $this->error($this->_ramodel->error);
                            }
                        }
                    }
                }
            }else{
                foreach (array_column($this->_ramodel->get_all_r(), 'aid') as $v){
                    $this->_ramodel->remove_ra($_GET['id'], $v);
                }
            }
            $this->success('设置成功！',__APP__.'?c=Authority&m=roleList');
        }
        $this->assign('firstMenu', $this->_amodel->get_all_cl(0));
        $this->assign('secondMenu', $this->_amodel->get_all_cl(1));
        $this->assign('thirdMenu', $this->_amodel->get_all_cl(2));
        $this->assign('radata', array_column($this->_ramodel->get_all_r(), 'aid'));
        $this->display();
    }
    public function accessList(){
        $data = $this->_amodel->get_one_page();
        $accessData = $data['returnData'];
        $this->assign('data', $data);
        $this->assign('accessData', $accessData);
        $this->display();
    }
    public function addAccess(){
        if(IS_POST){
            $p = array();
            if($_POST['level'] == 0){
                $p['icon_class'] = $_POST['icon_class'];
            }else{
                $p['parent_id'] = $_POST['parent_id'];
                $p['control'] = $_POST['control'];
                $p['method'] = $_POST['method'];
            }
            $p['title'] = $_POST['title'];
            $p['level'] = $_POST['level'];
            if(!$this->_amodel->add_data($p)){
                $this->error($this->_amodel->error);
            }
            $this->success('权限添加成功！',__APP__.'?c=Authority&m=accessList');
        }
        $this->display();
    }
    public function editAccess(){
        if (IS_POST){
            $p = array();
            if($_POST['level'] == 0){
                $p['icon_class'] = $_POST['icon_class'];
            }else{
                $p['parent_id'] = $_POST['parent_id'];
                $p['control'] = $_POST['control'];
                $p['method'] = $_POST['method'];
            }
            $p['title'] = $_POST['title'];
            $p['level'] = $_POST['level'];
            if(!$this->_amodel->update_data($p)){
                $this->error($this->_amodel->error);
            }
            $this->success('保存成功！',__APP__.'?c=Authority&m=accessList');
        }
        $oldData = $this->_amodel->get_one();
        $parentMenu = $this->_amodel->get_parents_all_level($oldData['level']-1);
        $this->assign('oldData', $oldData);
        $this->assign('parentMenu', $parentMenu);
//        print_r($parentMenu);die;
        $this->display();
    }
    public function accessDel(){
        $this->_amodel->del();
        $this->success('删除成功！', __APP__."?c=Authority&m=accessList");
    }
    public function getParentsIdByLevel(){
        if(isset($_POST['level'])){
            $level = $_POST['level'];
            if($level == 1){
                $data = $this->_amodel->get_parents_all_level(0);
            }
            if($level == 2){
                $data = $this->_amodel->get_parents_all_level(1);
            }
        }
        echo json_encode($data);
    }
}